home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gawk / cawf2st.zoo / macsup.c < prev    next >
C/C++ Source or Header  |  1992-04-12  |  5KB  |  182 lines

  1. /*
  2.  *      macsup.c - macro processing support functions for cawf(1)
  3.  */
  4.  
  5. /*
  6.  *      Copyright (c) 1991 Purdue University Research Foundation,
  7.  *      West Lafayette, Indiana 47907.  All rights reserved.
  8.  *
  9.  *      Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
  10.  *      University Computing Center.  Not derived from licensed software;
  11.  *      derived from awf(1) by Henry Spencer of the University of Toronto.
  12.  *
  13.  *      Permission is granted to anyone to use this software for any
  14.  *      purpose on any computer system, and to alter it and redistribute
  15.  *      it freely, subject to the following restrictions:
  16.  *
  17.  *      1. The author is not responsible for any consequences of use of
  18.  *         this software, even if they arise from flaws in it.
  19.  *
  20.  *      2. The origin of this software must not be misrepresented, either
  21.  *         by explicit claim or by omission.  Credits must appear in the
  22.  *         documentation.
  23.  *
  24.  *      3. Altered versions must be plainly marked as such, and must not
  25.  *         be misrepresented as being the original software.  Credits must
  26.  *         appear in the documentation.
  27.  *
  28.  *      4. This notice may not be removed or altered.
  29.  */
  30.  
  31. #include "cawf.h"
  32. #ifdef  UNIX
  33. #else
  34. #include <malloc.h>
  35. #endif
  36.  
  37.  
  38. /*
  39.  * Delmacro(mx) - delete macro
  40.  */
  41.  
  42. Delmacro(mx)
  43.         int mx;                         /* macro index */
  44. {
  45.         char buf[MAXLINE];              /* error message buffer */
  46.         int i, j;                       /* temporary indexes */
  47.  
  48.         if (mx >= Nmac) {
  49.                 (void) sprintf(buf, " bad Delmacro(%d) index", mx);
  50.                 Error(FATAL, LINE, buf, NULL);
  51.         }
  52.         for (i = Macrotab[mx].bx, j = i + Macrotab[mx].ct; i < j; i++) {
  53.                 Free(&Macrotxt[i]);
  54.         }
  55.         for (i = mx; i < (Nmac - 1); i++) {
  56.                 Macrotab[i] = Macrotab[i+1];
  57.         }
  58.         Nmac--;
  59. }
  60.  
  61.  
  62. /*
  63.  * Field(n, p, c) - skip to field n in p and optionally return a copy
  64.  */
  65.  
  66. char *
  67. Field(n, p, c)
  68.         int n;                          /* field number */
  69.         char *p;                        /* point to line containing fields */
  70.         int c;                          /* 1: make a copy of the field */
  71. {
  72.         char *fs, *fe, *s;
  73.  
  74.         if (c)
  75.                 Free(&F);
  76.         fe = p;
  77.         while (n) {
  78.                 while (*fe == ' ' || *fe == '\t')
  79.                         fe++;
  80.                 fs = fe;
  81.                 while (*fe && *fe != ' ' && *fe != '\t')
  82.                         fe++;
  83.                 if (fs == fe)
  84.                         return(NULL);
  85.                 if (n == 1) {
  86.                         if ( ! c)
  87.                                 return(fs);
  88.                         if ((F = malloc((size_t)(fe - fs + 1))) == NULL)
  89.                                 Error(FATAL, LINE, " Field out of string space",
  90.                                         NULL);
  91.                         (void) strncpy(F, fs, (fe - fs));
  92.                         F[fe -fs] = '\0';
  93.                         return(F);
  94.                 }
  95.                 n--;
  96.         }
  97.         return(NULL);
  98. }
  99.  
  100. /*
  101.  * Findmacro(p, e) - find macro and optionally enter it
  102.  *
  103.  * return = Macrotab[] index or -1 if not found
  104.  */
  105.  
  106.  
  107. Findmacro(p, e)
  108.         char *p;                /* pointer to 2 character macro name  */
  109.         int e;                  /* 0 = find, don't enter
  110.                                  * 1 = enter, don't find */
  111. {
  112.         char c[3];
  113.         int cmp, hi, low, mid;
  114.  
  115.         c[0] = p[0];
  116.         c[1] = (p[1] == ' ' || p[1] == '\t') ? '\0' : p[1];
  117.         c[2] = '\0';
  118.         low = mid = 0;
  119.         hi = Nmac - 1;
  120.         while (low <= hi) {
  121.                 mid = (low + hi) / 2;
  122.                 if ((cmp = strncmp(c, Macrotab[mid].name, 2)) < 0)
  123.                         hi = mid - 1;
  124.                 else if (cmp > 0)
  125.                         low = mid + 1;
  126.                 else {
  127.                         if ( ! e)
  128.                                 return(mid);
  129.                          Error(WARN, LINE, " duplicate macro ", c);
  130.                          hi = Macrotab[mid].bx + Macrotab[mid].ct;
  131.                          for (low = Macrotab[mid].bx; low < hi; low++) {
  132.                                 Free(&Macrotxt[low]);
  133.                          }
  134.                          goto new_macro;
  135.                 }
  136.         }
  137.         if ( ! e)
  138.                 return(-1);
  139.         if (Nmac >= MAXMACRO)
  140.                 Error(FATAL, LINE, " macro table full at ", c);
  141.         if (Nmac) {
  142.                 if (cmp > 0)
  143.                         mid++;
  144.                 for (hi = Nmac - 1; hi >= mid; hi--)
  145.                         Macrotab[hi+1] = Macrotab[hi];
  146.         }
  147.         Nmac++;
  148.         Macrotab[mid].name[0] = c[0];
  149.         Macrotab[mid].name[1] = c[1];
  150.  
  151. new_macro:
  152.  
  153.         Macrotab[mid].bx = -1;
  154.         Macrotab[mid].ct = 0;
  155.         return(mid);
  156. }
  157.  
  158. Free(p)
  159.         char **p;
  160. {
  161.         if (*p != NULL) {
  162.                 (void) free(*p);
  163.                 *p = NULL;
  164.         }
  165. }
  166.  
  167. /*
  168.  * Newstr(s) - allocate space for string
  169.  */
  170.  
  171. char *
  172. Newstr(s)
  173.         char *s;
  174. {
  175.         char *ns;
  176.  
  177.         if ((ns = malloc((size_t)(strlen(s) + 1))) == NULL)
  178.                 Error(FATAL, LINE, " Newstr out of malloc space at ", s);
  179.         (void) strcpy(ns, s);
  180.         return(ns);
  181. }
  182.